package cn.school.mybatis.test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.tools.ant.types.CommandlineJava.SysProperties;

import cn.school.mybatis.Dao.GradeDao;
import cn.school.mybatis.join.Grade;
import cn.school.mybatis.xml.MyBatisUtil;

public class GradeDaoTest {

	public static void main(String[] args) {
		// del();
		// update();
		// search();
		// add();
		find();
	}

	public static void search() {
		SqlSession session = MyBatisUtil.get();// 连接
		GradeDao gradeDao = session.getMapper(GradeDao.class);
		List<Grade> list = gradeDao.search();
		for (Grade a : list) {
			System.out.println(a);
		}
		// 关闭
		MyBatisUtil.close(session);
	}

	// 修改
	public static void update() {
		SqlSession session = MyBatisUtil.get();// 连接
		GradeDao gradeDao = session.getMapper(GradeDao.class);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", 12);
		map.put("gname", "java71");
		int number = gradeDao.update(map);
		System.out.println(number);
		MyBatisUtil.close(session);
	}

	// 删除
	public static void del() {
		SqlSession session = MyBatisUtil.get();// 连接
		GradeDao gradeDao = session.getMapper(GradeDao.class);
		int number = gradeDao.del(18);
		System.out.println(number);
		MyBatisUtil.close(session);

	}

	// 增加
	public static void add() {
		SqlSession session = MyBatisUtil.get();// 连接
		GradeDao gradeDao = session.getMapper(GradeDao.class);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", 20);
		map.put("gname", "java250");
		int number = gradeDao.add(map);
		System.out.println(number);
		MyBatisUtil.close(session);
	}

	// 作业四完成查询年级（grade）信息的 同时
	// 获得年级下的课程（subject）
	public static void find() {
		SqlSession session = MyBatisUtil.get();// 连接
		GradeDao gradeDao = session.getMapper(GradeDao.class);
		Grade g = gradeDao.find(1);
		System.out.println("年级信息为" + g);
		System.out.println("对应的科目信息为" + g.getSub());
		MyBatisUtil.close(session);
	}
}
